import { User } from "./types";
import { useTagsStore } from "./tags";

/**
 * 登录用户信息
 */
export const useUserStore = defineStore(
  "user",
  () => {
    const user = ref<User>();
    const tagsStore = useTagsStore();
    /**
     * 用户登录
     *
     * @param val 用户信息
     */
    const login = (val: User) => {
      user.value = val;
      tagsStore.load();
    };
    const logout = () => {
      user.value = undefined;
      // 重置状态
      tagsStore.$reset();
    };

    const isLogin = computed(() => {
      return !!user.value;
    });
    const avatar = computed(() => {
      return user.value?.avatar
        ? `/avatars/${user.value?.avatar}`
        : `/api/avatar/${user.value?.nickname}.svg`;
    });
    return {
      user,
      login,
      logout,
      isLogin,
      avatar,
    };
  },
  {
    persist: {
      storage: sessionStorage,
    },
  },
);
